package battleModel;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.*;

import view.CampaignView;
import view.MasterView;

 
public class BattleModel {

	private Tank player;
	private Missile playerMissile;
	private ArrayList<Obstacle> obstacles;
	
	private Rectangle battleWindow;
	private GraphicsEnvironment ge;
	
	public BattleModel(Tank tank){
		this.player = tank;
		obstacles=new ArrayList<Obstacle>();
		
		ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		battleWindow = ge.getMaximumWindowBounds();
		
		
		double xScale, yScale;
		xScale=(double) battleWindow.width / 1300;
		yScale=(double) battleWindow.height / 950;
		obstacles.add(new OuterWall("North",xScale,yScale));
		obstacles.add(new OuterWall("South",xScale,yScale));
		obstacles.add(new OuterWall("East",xScale,yScale));
		obstacles.add(new OuterWall("West",xScale,yScale));
	}
	
	public Tank getPlayer(){
		return player;
	}
	
	public Rectangle getBattleWindow(){
		return battleWindow;
	}
	
	public Iterator<Obstacle> getObstacleItr(){
		return obstacles.iterator();
	}
	
	public Obstacle checkCollision(){
		for(Obstacle obs:obstacles){
			if(Path2D.intersects(player.getCollisionShape().getPathIterator(
					player.getTankAffine()), (Rectangle2D) obs.getCollisionShape())){
				player.receiveDamage(1);
				return obs;
			}
			
			
		}
		
		
		return null;
	}
	
	public boolean keyPressed(KeyEvent e) {

		//tank keys
		if (e.getKeyCode() == KeyEvent.VK_W) {
			if(checkCollision()==null){
				player.moveForward();
				if(checkCollision()!=null){
					player.moveBackward();
					player.moveBackward();
				}
			}
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			if(checkCollision()==null){
				player.moveBackward();
				if(checkCollision()!=null)
					player.moveForward();
			}
		} else if (e.getKeyCode() == KeyEvent.VK_D) {
			if(checkCollision()==null){
				player.turnRight();
				if(checkCollision()!=null)
					player.turnLeft();
			}
		} else if (e.getKeyCode() == KeyEvent.VK_A) {
			if(checkCollision()==null){
				player.turnLeft();
				if(checkCollision()!=null)
					player.turnRight();
			}
		}
		
		//turret keys
		else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			player.turretTurnRight();
		} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			player.turretTurnLeft();
		}
		else return false;
		
		return true;
		
	}
	
//	public boolean mouseClicked(MouseEvent e){
//		Missile missile=new Missile(missileImage, player.getTurretAngle(), player.getTurretAffine());
//		missile.getAffine().scale((double) battleWindow.width / 1300, (double) battleWindow.height / 950);
//		missiles.add(missile);
//	}
	
}
